package com.bjpowernode.web;

import com.bjpowernode.model.TActivityRemark;
import com.bjpowernode.model.TCustomerRemark;
import com.bjpowernode.query.ActivityRemarkQuery;
import com.bjpowernode.query.CustomerRemarkQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.CustomerRemarkService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
public class CustomerRemarkController {

    @Resource
    private CustomerRemarkService customerRemarkService;

    @PostMapping(value = "/api/customer/remark")
    public R addCustomerRemark(@RequestBody CustomerRemarkQuery customerRemarkQuery, @RequestHeader(value = "Authorization") String token) {
        //axios提交post请求，提交过来的是json数据，使用@RequestBody注解接收
        customerRemarkQuery.setToken(token);
        int save = customerRemarkService.saveCustomerRemark(customerRemarkQuery);
        return save >= 1 ? R.OK( ) : R.FAIL();
    }

    @GetMapping(value = "/api/customer/remark")
    public R customerRemarkPage(@RequestParam(value = "current", required = false) Integer current,
                                @RequestParam(value = "customerId") Integer customerId) {

        CustomerRemarkQuery customerRemarkQuery = new CustomerRemarkQuery();
        customerRemarkQuery.setCustomerId(customerId);

        if (current == null) {
            current = 1;
        }
        PageInfo<TCustomerRemark> pageInfo = customerRemarkService.getCustomerRemarkByPage(current, customerRemarkQuery);
        return R.OK(pageInfo);
    }

    @GetMapping(value = "/api/customer/remark/{id}")
    public R customerRemarkPage(@PathVariable(value = "id") Integer id) {
        TCustomerRemark tCustomerRemark = customerRemarkService.getCustomerRemarkById(id);
        return R.OK(tCustomerRemark);
    }

    @PutMapping(value = "/api/customer/remark")
    public R editCustomerRemark(@RequestBody CustomerRemarkQuery customerRemarkQuery, @RequestHeader(value = "Authorization") String token) {
        //axios提交post请求，提交过来的是json数据，使用@RequestBody注解接收
        customerRemarkQuery.setToken(token);
        int update = customerRemarkService.updateCustomerRemark(customerRemarkQuery);
        return update >= 1 ? R.OK( ) : R.FAIL();
    }

    @DeleteMapping(value = "/api/customer/remark/{id}")
    public R delCustomerRemark(@PathVariable(value = "id") Integer id) {
        int del =customerRemarkService.delCustomerRemarkById(id);
        return del >= 1 ? R.OK( ) : R.FAIL();
    }
}
